package display;

import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import book.Book;

import adoption.Adoption;

import java.awt.Dimension;

public class ImportDialog extends JDialog {
	private JButton btnClose;
	
	private ReentrantReadWriteLock tableLock = new ReentrantReadWriteLock(),
								   booksLock = new ReentrantReadWriteLock();
	private Deque<Book> books = new ArrayDeque<Book>();
	private JTable table;
	private JScrollPane scrollPane;

	/**
	 * Create the dialog.
	 */
	public ImportDialog() {
		setTitle("Importing");
		setBounds(100, 100, 420, 200);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		btnClose = new JButton("Close");
		btnClose.setEnabled(false);
		btnClose.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				ImportDialog.this.dispose();
			}
			
		});
		
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		getContentPane().add(scrollPane, gbc_scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"File Name", "Count", "Status"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, Integer.class, Object.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		GridBagConstraints gbc_btnClose = new GridBagConstraints();
		gbc_btnClose.gridx = 0;
		gbc_btnClose.gridy = 1;
		getContentPane().add(btnClose, gbc_btnClose);

	}
	
	public void addFile(String f) {
		tableLock.writeLock().lock();
		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		
		dtm.addRow(new String[]{f,"0","Opening"});
		dtm.fireTableDataChanged();
		validate();
		tableLock.writeLock().unlock();
	}
	
	public void setCount(String f, int c) {
		int index = getRowNum(f);
		
		tableLock.writeLock().lock();
		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		
		if (index < dtm.getRowCount())
			dtm.setValueAt(String.valueOf(c), index, 1);
		tableLock.writeLock().unlock();
	}
	
	private int getRowNum(String f) {
		tableLock.readLock().lock();
		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		int i = 0;
		
		for (; i < dtm.getRowCount(); ++i)
			if ( ((String)dtm.getValueAt(i, 0)).equals(f))
				break;
		int out = (i < dtm.getRowCount()? i:-1);
		tableLock.readLock().unlock();
		return out;
	}
	
	public void setStatus(String f, String stat) {
		int index = getRowNum(f);

		tableLock.writeLock().lock();
		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		dtm.setValueAt(stat, index, 2);
		tableLock.writeLock().unlock();
		
	}
	
	public void insert(ArrayList<Book> in) {
		booksLock.writeLock().lock();
		books.addAll(in);
		booksLock.writeLock().unlock();
	}
	
	public boolean finished() {
		tableLock.readLock().lock();
		boolean out = true;
		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		for (int i = 0; i < dtm.getRowCount(); ++i)
			out &= dtm.getValueAt(i, 2).equals("Finished");
		tableLock.readLock().unlock();
		
		return out;
	}

}
